JavaScript

Check Console for Output

For Explanation Click Here

for-in, for-of, arrays, strings and objects in JavaScript

1.Solve using for in loop.
    var car = {
      brand: 'Toyota',
      model: 'Corolla',
      year: 2020
    };
    for (i in car) {
      console.log(`${i}: ${car[i].toUpperCase()}`);
    }
A:Output:
brand: TOYOTA
model: COROLLA
year: 2020

2.Solve using for in loop.
    var numbers = [1, 2, 3, 4, 5];
    for (i in numbers) {
      console.log(`${numbers[i]} - ${"HI"}`);
    }
A:Output:
1-HI
2-HI
3-HI
4-HI
5-HI

3.Solve using for in loop.
    var fruits = ['Apple', 'Banana', 'Cherry', 'Date'];
    for (i in fruits) {
      console.log(`${i}-${fruits[i].toUpperCase()}`);
    }
A:Output:
0-APPLE
1-BANANA
2-CHERRY
3-DATE

4.Update the city property here
    var obj = {
      name: 'John',
      age: 30,
      address: {
        city: 'Los Angeles',
        state: 'CA'
      }
    }
    obj.address.city = 'San Francisco';
    console.log(obj);
A:Output: {name: 'John', age: 30, address: {city: 'San Francisco', state: 'CA'}}

5.Update the model and year here.
    var car = {
      brand: 'Toyota',
      model: 'Camry',
      year: 2020
    };
    car.model = 'Corolla';
    car.year = 2022;
    console.log(car);
A:Output: {brand: 'Toyota', model: 'Corolla', year: 2022}

6.Add an ingredient here.
    var recipe = {
      name: 'Pasta',
      servings: 2,
      ingredients: ['noodles', 'sauce']
    };
    recipe.ingredients.push('cheese');
    console.log(recipe);
A:Output: {name: 'Pasta', servings: 2, ingredients: 
['noodles', 'sauce', 'cheese']}